//
//  DECXDriversListViewController.m
//  DECXInStore
//
//  Created by 李坤 on 2018/9/7.
//  Copyright © 2018年 程强. All rights reserved.
//

#import "DECXDriversListViewController.h"
#import "DECXDriverTableCell.h"
#import "DECXDriverAddViewController.h"
#import "DECXManager.h"
#import "DECXDriverObj.h"
#import "DECXWarehouseChargeManager.h"

@interface DECXDriversListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *driverTable;
@property (strong, nonatomic) DECXManager *manager;
@property (strong, nonatomic) DECXWarehouseChargeManager *wManager;
@property (strong, nonatomic) NSArray *driverAry;
@property (strong, nonatomic) void(^choseBlock)(NSArray *ary);
@end

@implementation DECXDriversListViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([DECXGlobal shareInstance].loginType.integerValue == 2) {
        [self fetchDecxUserDriversList];
    }else{
        [self fetchDriversList];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setRightBarItem];
    self.manager = [DECXManager new];
    self.wManager = [DECXWarehouseChargeManager new];
}
- (void)setRightBarItem
{
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addClick)];
    self.navigationItem.rightBarButtonItem = item;
}
- (void)addClick
{
    DECXDriverAddViewController *vc = StoryBoardViewController(@"Main", @"DECXDriverAddViewController");
    [self.navigationController pushViewController:vc animated:YES];
}
- (NSArray *)getSelectedDrivers
{
    NSMutableArray *tempAry = @[].mutableCopy;
    for (DECXDriverObj *obj in self.driverAry) {
        if (obj.isSelected) {
            [tempAry addObject:obj];
        }
    }
    return tempAry;
}
- (IBAction)choseCompleteClick:(id)sender
{
    if (self.choseBlock) {
        self.choseBlock([self getSelectedDrivers]);
        [self gotoBack];
    }
}
- (void)choseDriversHandler:(void(^)(NSArray *ary))block
{
    self.choseBlock = block;
}
#pragma mark --- 仓管用户司机列表
- (void)fetchDriversList
{
    MyUser;
    weak_Self(self);
    [[self.manager driverList:user.depot_id]subscribeNext:^(id x) {
        weakSelf.driverAry = x;
        [weakSelf.driverTable reloadData];
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        ToastShowInWindow(msg);
    } completed:^{
        
    }];
}
#pragma mark --- decx用户司机列表
- (void)fetchDecxUserDriversList
{
    MyUser;
    weak_Self(self);
    [[self.wManager decxUserDriverList:user.userid]subscribeNext:^(id x) {
        weakSelf.driverAry = x;
        [weakSelf.driverTable reloadData];
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        ToastShowInWindow(msg);
    } completed:^{
        
    }];
}
- (void)deleteDriver:(NSString *)xid success:(void(^)(void))block
{
    [[self.manager driverDelete:xid]subscribeNext:^(id x) {
        if (block) {
            block();
        }
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        ToastShowInWindow(msg);
    } completed:^{
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.driverAry.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"DriverCell";
    
    DECXDriverTableCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell) {
        cell = [[DECXDriverTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    DECXDriverObj *driver = self.driverAry[indexPath.row];
    cell.name.text = driver.user_name;
    cell.carNo.text = driver.car_code;
    if (driver.isSelected) {
        cell.selectMark.highlighted = YES;
    }else{
        cell.selectMark.highlighted = NO;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DECXDriverObj *driver = self.driverAry[indexPath.row];
    driver.isSelected = !driver.isSelected;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        weak_Self(self);
        DECXDriverObj *driver = self.driverAry[indexPath.row];
        [self deleteDriver:driver.xid success:^{
            NSMutableArray *temp = weakSelf.driverAry.mutableCopy;
            [temp removeObject:driver];
            weakSelf.driverAry = temp;
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
